home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter6 / finditem.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  10KB  |  297 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "finditem.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "App";
  20. LPCTSTR lpszTitle   = "ListView_FindItem()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LPCTSTR lpszData[4]  = { "Circle", "Rectangle", "Cross", "Check" };
  108. LPCTSTR lpszColor[4] = { "Yellow", "Red",       "Black", "Black" };
  109.  
  110. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  111. {
  112. static char  szBuf[40];
  113. static UINT  uTimerID = 0;
  114. static HWND  hList    = NULL;
  115. static HWND  hStatic  = NULL;
  116.  
  117.    switch( uMsg )
  118.    {
  119.       case WM_CREATE  :
  120.               {
  121.                  HIMAGELIST hImage, hSmall;
  122.  
  123.                  InitCommonControls();
  124.  
  125.                  // Create the image lists and the list view.
  126.                  //..........................................
  127.                  hImage = ImageList_Create( 32, 32, ILC_COLOR4 | ILC_MASK, 4, 1 ); 
  128.                  hSmall = ImageList_Create( 16, 16, ILC_COLOR4 | ILC_MASK, 4, 1 );
  129.                  hList  = CreateWindowEx( 0, WC_LISTVIEW, "",
  130.                                           WS_CHILD | WS_VISIBLE | LVS_REPORT | 
  131.                                           LVS_EDITLABELS | LVS_SINGLESEL,
  132.                                           0, 20, 100, 100,
  133.                                           hWnd,
  134.                                           (HMENU)1,
  135.                                           hInst,
  136.                                           NULL);
  137.  
  138.                  hStatic = CreateWindowEx( 0, "STATIC", "", WS_CHILD | WS_VISIBLE,
  139.                                            0, 0, 100, 20,
  140.                                            hWnd,
  141.                                            (HMENU)2,
  142.                                            hInst,
  143.                                            NULL );
  144.  
  145.                  if ( hList && hImage && hSmall )
  146.                  {
  147.                     LV_COLUMN col;
  148.                     LV_ITEM   item;
  149.                     int       i;
  150.  
  151.                     // Set the large and small image lists.
  152.                     //.....................................
  153.                     ListView_SetImageList( hList, hImage, LVSIL_NORMAL );
  154.                     ListView_SetImageList( hList, hSmall, LVSIL_SMALL );
  155.  
  156.                     // Add the images to the image list.
  157.                     //..................................
  158.                     for( i=0; i<4; i++ )
  159.                     {
  160.                        ImageList_AddIcon( hImage, LoadIcon( hInst, lpszData[i] ) );
  161.                        ImageList_AddIcon( hSmall, LoadIcon( hInst, lpszData[i] ) );
  162.                     }
  163.  
  164.                     // Add the columns for the report view.
  165.                     //.....................................
  166.                     col.mask    = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; 
  167.                     col.fmt     = LVCFMT_LEFT; 
  168.                     col.cx      = 100; 
  169.  
  170.                     col.pszText  = "Name"; 
  171.                     col.iSubItem = 0;
  172.                     ListView_InsertColumn( hList, 0, &col );
  173.                     
  174.                     col.pszText  = "Color"; 
  175.                     col.iSubItem = 1;
  176.                     ListView_InsertColumn( hList, 1, &col );
  177.  
  178.                     // Insert items into the list view.
  179.                     //.................................
  180.                     for( i=0; i<4; i++ )
  181.                     {
  182.                        item.mask     = LVIF_TEXT | LVIF_IMAGE;
  183.                        item.pszText  = (LPTSTR)lpszData[i];
  184.                        item.iItem    = i;
  185.                        item.iSubItem = 0;
  186.                        item.iImage   = i;
  187.                        ListView_InsertItem( hList, &item );
  188.  
  189.                        item.mask     = LVIF_TEXT;
  190.                        item.pszText  = (LPTSTR)lpszColor[i];
  191.                        item.iSubItem = 1;
  192.                        ListView_SetItem( hList, &item );
  193.                     }
  194.                  }
  195.  
  196.                  uTimerID = SetTimer( hWnd, 1, 500, NULL );
  197.               }
  198.               break;
  199.  
  200.       case WM_SIZE :
  201.               if ( hStatic )
  202.                  MoveWindow( hStatic, 0, 0, LOWORD( lParam ), 20, FALSE );
  203.  
  204.               if ( hList )
  205.                  MoveWindow( hList, 0, 20, LOWORD( lParam ), HIWORD( lParam )-20, FALSE );
  206.  
  207.               if ( wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED )
  208.                  ListView_RedrawItems( hList, 0, ListView_GetItemCount( hList ) );
  209.               break;
  210.  
  211.  
  212.       case WM_TIMER :
  213.               if ( wParam == uTimerID )
  214.               {
  215.                  ListView_GetISearchString( hList, szBuf );
  216.                  SetWindowText( hStatic, szBuf );
  217.               }
  218.               break;
  219.  
  220.       case WM_COMMAND :
  221.               switch( LOWORD( wParam ) )
  222.               {
  223.                  case IDM_FIND :
  224.                         {
  225.                            LV_FINDINFO fi;
  226.                            int         nItem = -1;       
  227.  
  228.                            fi.flags = LVFI_STRING;
  229.                            fi.psz   = "Rectangle";
  230.  
  231.                            nItem = ListView_FindItem( hList, -1, &fi );
  232.  
  233.                            if ( nItem > -1 &&
  234.                                 !(ListView_GetItemState( hList, nItem,
  235.                                              LVIS_SELECTED) & LVIS_SELECTED) )
  236.                            {
  237.                               ListView_EnsureVisible( hList, nItem, FALSE );
  238.                               ListView_SetItemState( hList, nItem, 
  239.                                                      LVIS_SELECTED | LVIS_FOCUSED, 
  240.                                                      LVIS_SELECTED | LVIS_FOCUSED); 
  241.                            }
  242.                         }
  243.                         break;      
  244.  
  245.                  case IDM_ABOUT :
  246.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  247.                         break;
  248.  
  249.                  case IDM_EXIT :
  250.                         DestroyWindow( hWnd );
  251.                         break;
  252.               }
  253.               break;
  254.       
  255.       case WM_DESTROY :
  256.               KillTimer( hWnd, uTimerID );
  257.  
  258.               if ( ListView_GetImageList( hList, LVSIL_NORMAL ) )
  259.                  ImageList_Destroy( ListView_GetImageList( hList, LVSIL_NORMAL ) );
  260.  
  261.               if ( ListView_GetImageList( hList, LVSIL_SMALL ) )
  262.                  ImageList_Destroy( ListView_GetImageList( hList, LVSIL_SMALL ) );
  263.  
  264.               PostQuitMessage(0);
  265.               break;
  266.  
  267.       default :
  268.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  269.    }
  270.  
  271.    return( 0L );
  272. }
  273.  
  274.  
  275. LRESULT CALLBACK About( HWND hDlg,           
  276.                         UINT message,        
  277.                         WPARAM wParam,       
  278.                         LPARAM lParam)
  279. {
  280.    switch (message) 
  281.    {
  282.        case WM_INITDIALOG: 
  283.                return (TRUE);
  284.  
  285.        case WM_COMMAND:                              
  286.                if (   LOWORD(wParam) == IDOK         
  287.                    || LOWORD(wParam) == IDCANCEL)    
  288.                {
  289.                        EndDialog(hDlg, TRUE);        
  290.                        return (TRUE);
  291.                }
  292.                break;
  293.    }
  294.  
  295.    return (FALSE); 
  296. }
  297.